package pl.poznan.put.cs.idss.students.kingcart.ui;

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.util.logging.Logger;

import javax.swing.JFrame;

import com.googlecode.javacv.cpp.opencv_core.IplImage;

import pl.poznan.put.cs.idss.students.kingcart.core.capturer.Capturer;
import pl.poznan.put.cs.idss.students.kingcart.core.capturer.VideoCapturer;
import pl.poznan.put.cs.idss.students.kingcart.core.capturer.WebCamCapturer;
import pl.poznan.put.cs.idss.students.kingcart.core.model.Features;
import pl.poznan.put.cs.idss.students.kingcart.core.model.FrameFeatures;
import pl.poznan.put.cs.idss.students.kingcart.core.tracker.SimpleTracker;
import pl.poznan.put.cs.idss.students.kingcart.core.tracker.Tracker;
import pl.poznan.put.cs.idss.students.kingcart.core.tracker.TrackerWithConstantBackground;
import pl.poznan.put.cs.idss.students.kingcart.i18n.Messages;
import pl.poznan.put.cs.idss.students.kingcart.properties.Properties;

/**
 * Main application window. Contains canvas object and toolbox.
 * 
 * @author Kornel Lewandowski
 * 
 */
public class GeneralWindow extends JFrame {

	private static final long serialVersionUID = -6774540003020942443L;

	Logger log = Logger.getLogger("pl.poznan.put.cs.idss.students.kingcart");

	private boolean isReady = false;

	private MainCanvas mainCanvas = new MainCanvas(this);
	private Toolbox toolbox = new Toolbox(this);

	Image icon = Toolkit.getDefaultToolkit()
			.getImage(
					GeneralWindow.class.getResource(Properties
							.get("application_icon")));

	private Capturer capturer;
	// private Tracker tracker = new SimpleTracker();
	private Tracker tracker = new TrackerWithConstantBackground();
	private Features features = new FrameFeatures();

	public GeneralWindow(int width, int height) {

		setSize(width, height);
		setLocationRelativeTo(null);
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setTitle(Messages.get("UI.application_name") + " "
				+ Messages.get("UI.application_version"));
		setIconImage(icon);
		setVisible(false);

		prepareToolbox();
		prepareCanvas();

		isReady = true;

	}

	public void changeSource(int i) {
		if (i == 0)
			stop();
		else
			startWebCamCapture();
	}

	public void startVideoCapture(String fileName) {
		stop();
		capturer = new VideoCapturer();
		((VideoCapturer) capturer).setFileName(fileName);
		capturer.start(mainCanvas);
	}

	public void startWebCamCapture() {
		stop();
		capturer = new WebCamCapturer();
		capturer.start(mainCanvas);
	}

	public void prepareToolbox() {
		add(toolbox);
		log.info("Toolbox is prepared");
	}

	public void prepareCanvas() {
		add(mainCanvas);
		log.info("Canvas is prepared");
	}

	public void showWindow() {
		setVisible(true);
	}

	public boolean isReady() {
		return isReady;
	}

	public MainCanvas getCanvas() {
		return mainCanvas;
	}

	public void stop() {
		if (capturer != null) {
			capturer.stop();
		}
	}

	public void resume() {
		tracker = new TrackerWithConstantBackground();
		IplImage img = IplImage.createFrom((BufferedImage) mainCanvas
				.getImage());
		features.setFeatures(img, mainCanvas.getSelectedArea());
		tracker.setFeatures(features);

		capturer.resumeAndTrack(tracker);
	}

	public void snapshot() {
		stop();
		mainCanvas.isSelectionPossible(true);
	}

}
